//
// Created by xusun on 2022/1/28.
//

#include "iostream"

using namespace std;

class Solution {
private:
    string reverse(string s, int start, int end) {
        for (int i = 0; i <= (end - start) / 2; i++) {
            char tmp = s[start + i];
            s[start + i] = s[end - i];
            s[end - i] = tmp;
        }
        return s;
    }

public:
    string reverseWords(string s) {
        int pos = 0;
        int start = 0;
        int end = 0;
        while (s[pos] != '\0' && start < s.length()) {
            if (s[pos + 1] == ' ' || s[pos + 1] == '\0') {
                end = pos;
                s = reverse(s, start, end);
                start = pos + 2;
            }
            pos++;
        }
        return s;
    }
};